package com.liu.blindBox.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.liu.blindBox.entity.qo.SaleInfoQO;
import com.liu.blindBox.service.BuyBlindBoxService;
import com.liu.common.entity.Result;
import com.liu.common.entity.StatusCode;
import com.liu.common.service.GetTokenService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "http://localhost:8080", maxAge = 3600)
@RestController
@RequestMapping("/buy")
public class BuyBlindBoxController {

    @Resource
    private BuyBlindBoxService buyBlindBoxService;

    @Resource
    private GetTokenService getTokenService;

    @PostMapping("/buyBlindBox")
    public Result buyBlindBox(@RequestBody List<SaleInfoQO> saleInfoQOList){
        String token = getTokenService.getToken();
        Object loginIdByToken = StpUtil.getLoginIdByToken(token);
        if (loginIdByToken != null){
            int i = buyBlindBoxService.buyBlindBox(saleInfoQOList, String.valueOf(loginIdByToken));
            if (i == 0){
                return new Result(false, StatusCode.ERROR,"余额不足");
            }else {
                return new Result(true, StatusCode.OK,"购买成功");
            }
        }else {
            return new Result(false, StatusCode.ERROR,"请登录后再尝试购买");
        }
    }

}
